package cn.plushine.controller;

import cn.plushine.pojo.POrder;
import cn.plushine.pojo.POrderItem;
import cn.plushine.service.BaseInputService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 入库
 */
@Api(tags = "入库订单")
@Controller
@RestController
@RequestMapping("/admin/product/input")
public class BaseInputController {

    @Autowired
    BaseInputService baseInputService;

    // 获取入库订单
    @ApiOperation(value = "获取入库订单")
    @GetMapping("/get")
    public HashMap<String, Object> get() {
        try {
            // 获取入库订单
            List<POrder> pOrdersDB = baseInputService.get();

            // 货物总数（sum），状态判断（flag）
            int sum, flag;
            for (POrder pOrder : pOrdersDB) {
                sum = pOrder.getPOrders().size();
                flag = 0;
                for (POrderItem order : pOrder.getPOrders()) {
                    flag += order.getFlag();
                }
                pOrder.setSum(sum);
                // 订单状态判断
                pOrder.setFlag(flag == 0 ? 0 : (flag == sum ? 2 : 1));
            }
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取入库订单成功！", pOrdersDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取入库订单失败！", e.getMessage());
        }
    }

    // 批量入库
    @ApiOperation(value = "批量入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keeper", value = "入库人id", paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "warehouse", value = "仓库id", paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "area", value = "分区id", paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "position", value = "库位id", paramType = "path", dataType = "int", example = "1"),
            @ApiImplicitParam(name = "multiplySelect", value = "批量入库的货物信息", paramType = "body", dataType = "ArrayList<HashMap>")
    })
    @PutMapping("/add/{keeper}/{inputId}/{warehouse}/{area}/{position}")
    public HashMap<String, Object> update(@PathVariable int keeper, @PathVariable String inputId, @PathVariable int warehouse, @PathVariable int area, @PathVariable int position, @RequestBody ArrayList<HashMap> multiplySelect) {
        try {
            // 批量入库
            baseInputService.update(warehouse, area, position, multiplySelect);
            // 入库时间和入库人信息
            baseInputService.modify(inputId, keeper);
            // 成功响应
            return ResMapUtils.encapsulation(200, "批量入库成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "批量入库失败！", e.getMessage());
        }
    }
}
